import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: () => import('@/views/LoginView.vue')
  }, {
    path: '/home',
    name: 'home',
    component: () => import('@/views/HomeView.vue'),
    children: [
      {
        path: 'userinfo',
        name: 'userinfo',
        component: () => import('@/views/userinfo/UserinfoList.vue')
      },
      {
        path: 'role',
        name: 'role',
        component: () => import('@/views/role/RoleList.vue')
      },
      {
        path: 'category',
        name: 'category',
        component: () => import('@/views/category/CategoryList.vue')
      },
      {
        path: 'film',
        name: 'film',
        component: () => import('@/views/film/FilmList.vue')
      },
      {
        path: 'actor',
        name: 'actor',
        component: () => import('@/views/actor/ActorList.vue')
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  const whileList = ['/']
  let go = false
  for (const wl of whileList) {
    if (wl === to.path) {
      go = true
      break
    }
  }
  if (go) {
    next()
  } else {
    const loginInfo = sessionStorage.getItem('loginInfo')
    if (loginInfo) {
      next()
    } else {
      next('/')
    }
  }
  next()
})

export default router
